#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author:BINGO
# datetime:2018/10/30 下午 05:47
# software: PyCharm
from code2.Detail import Detail
import requests
import json
import random
from bs4 import BeautifulSoup


def GetUserAgent():
    '''
    功能：随机获取HTTP_User_Agent
    '''
    user_agents = [
        "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; AcooBrowser; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
        "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Acoo Browser; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506)",
        "Mozilla/4.0 (compatible; MSIE 7.0; AOL 9.5; AOLBuild 4337.35; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
        "Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US)",
        "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 2.0.50727; Media Center PC 6.0)",
        "Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 1.0.3705; .NET CLR 1.1.4322)",
        "Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.30)",
        "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN) AppleWebKit/523.15 (KHTML, like Gecko, Safari/419.3) Arora/0.3 (Change: 287 c9dfb30)",
        "Mozilla/5.0 (X11; U; Linux; en-US) AppleWebKit/527+ (KHTML, like Gecko, Safari/419.3) Arora/0.6",
        "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2pre) Gecko/20070215 K-Ninja/2.1.1",
        "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/20080705 Firefox/3.0 Kapiko/3.0",
        "Mozilla/5.0 (X11; Linux i686; U;) Gecko/20070322 Kazehakase/0.4.5",
        "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.8) Gecko Fedora/1.9.0.8-1.fc10 Kazehakase/0.5.6",
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11",
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.20 (KHTML, like Gecko) Chrome/19.0.1036.7 Safari/535.20",
        "Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; fr) Presto/2.9.168 Version/11.52",
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.11 TaoBrowser/2.0 Safari/536.11",
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.71 Safari/537.1 LBBROWSER",
        "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; LBBROWSER)",
        "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E; LBBROWSER)",
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.84 Safari/535.11 LBBROWSER",
        "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)",
        "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; QQBrowser/7.0.3698.400)",
        "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E)",
        "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SV1; QQDownload 732; .NET4.0C; .NET4.0E; 360SE)",
        "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E)",
        "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)",
        "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1",
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1",
        "Mozilla/5.0 (iPad; U; CPU OS 4_2_1 like Mac OS X; zh-cn) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148 Safari/6533.18.5",
        "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:2.0b13pre) Gecko/20110307 Firefox/4.0b13pre",
        "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:16.0) Gecko/20100101 Firefox/16.0",
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11",
        "Mozilla/5.0 (X11; U; Linux x86_64; zh-CN; rv:1.9.2.10) Gecko/20100922 Ubuntu/10.10 (maverick) Firefox/3.6.10"
    ]
    user_agent = random.choice(user_agents)
    return user_agent


def getIp():
    ipArr = [
        'http://182.88.134.130:8123',
        'http://119.5.1.14:808',
        'http://175.148.72.60:1133',
        'http://171.37.152.66:8123',
        'http://182.88.89.145:8123',
        'http://171.37.152.29:8123',
        'http://115.148.248.29:8118',
        'http://118.89.138.129:52699',
        'http://49.71.81.62:3128',
        'http://117.68.192.188:808',
        'http://58.48.168.166:51430',
        'http://182.88.189.231:8123',
        'http://114.225.169.161:53128',
        'http://222.212.88.12:56389',
        'http://113.103.12.13:3128',
        'http://182.88.14.212:8123',
        'http://171.39.74.157:8123',
        'http://115.223.96.38:8010',
        'http://180.110.7.53:808',
        'http://182.111.64.7:41766',
        'http://115.223.77.203:8010',
        'http://175.155.24.3:808',
        'http://115.223.70.87:8010',
        'http://114.217.3.178:808',
        'http://115.46.79.183:8123',
        'http://182.88.15.127:8123',
        'http://115.46.72.246:8123',
        'http://218.76.253.201:61408',
        'http://115.46.103.130:8123',
        'http://182.88.164.233:8123',
        'http://115.223.87.172:8010',
        'http://60.190.66.131:56882',
        'http://1.197.153.251:8010',
        'http://222.242.155.69:40919',
        'http://115.46.68.250:8123',
        'http://60.205.213.172:8118',
        'http://221.234.192.73:8010',
        'http://182.88.186.233:8123',
        'http://123.53.132.123:8010',
        'http://175.148.69.25:1133',
        'http://59.32.37.56:8010',
        'http://182.88.190.147:8123',
        'http://115.223.125.85:8010',
        'http://182.88.129.29:8123',
        'http://117.78.35.7:808',
        'http://171.38.42.23:8123',
        'http://182.88.187.199:8123',
        'http://119.5.1.22:808',
        'http://119.4.13.26:1133',
        'http://121.31.194.30:8123',
    ]

    ip = random.choice(ipArr)
    return ip


def findStreetDetailByAnJuKe(detail, cTime):
    detailByStreet = []
    try:
        headers = {
            'User-Agent': GetUserAgent(),
            # #'Host': 'www.lagou.com',
            # 'Referer': 'https://chengdu.anjuke.com/',
            # 'authority': 'chengdu.anjuke.com',
            # 'upgrade-insecure-requests':'1',
        }
        ip = getIp();
        proxies = {
            'http': ip,
        }
        url = 'https://chengdu.anjuke.com/sale/rd1/?from=zjsr&kw=%s' % detail.street

        print('查询的url 地址：===》%s' % url)
        print('查询的代理地址：===》%s' % ip)
        resDatas = requests.post(url, headers=headers, proxies=proxies)
        soup = BeautifulSoup(resDatas.text, 'html.parser')

        findList = soup.find('ul', class_='houselist-mod-new')
        house_list = findList.find_all('li', class_='list-item')

        idschool = detail.idschool

        for house in house_list:
            d = Detail()
            d.time = cTime
            d.idschool = idschool
            d.street = detail.street
            name = house.find('div', class_='house-title').a.text.strip()
            d.title = name

            img = house.find('div', class_='item-img').find('img').get('src')
            d.img1 = img

            url = house.find('div', class_='house-title').a.get('href')
            d.url = url

            price = house.find('span', class_='price-det').text.strip()
            d.price = price
            unitPrice = house.find('span', class_='unit-price').text.strip()
            d.unitPrice = unitPrice
            price_area = house.find('span', class_='unit-price').text.strip()  # 单位面积
            d.acreage = price_area
            no_room = house.find('div', class_='details-item').span.text  # 几室几厅
            d.houseType = no_room
            area = house.find('div', class_='details-item').contents[3].text
            d.area = area
            floor = house.find('div', class_='details-item').contents[5].text
            d.floor = floor  #
            year = house.find('div', class_='details-item').contents[7].text

            # 详细地址
            streetDetail = house.find('span', class_='comm-address').text
            d.streetDetail = streetDetail

            broker = house.find('span', class_='brokername').text
            broker = broker[1:]

            address = house.find('span', class_='comm-address').text.strip()
            address = address.replace('\xa0\xa0\n', ' ')

            tag_list = house.find_all('span', class_='item-tags')
            tags = [i.text for i in tag_list]
            d.tag = tags
            d.dataFrom = '安居客'
            detailByStreet.append(d)
    except Exception as e:
        print(e)
    finally:
        return detailByStreet
